<?php
/**
 * 音视频服务
 * @date    2021-04-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
/**
rights Reserved, Designed By www.youyacao.com
@author:成都市一颗优雅草科技有限公司
@version 优雅草蜻蜓系统-服务端部分
注意：服务端代码在获得授权之前通过其他非官方渠道获得代码均为侵权，禁止用于商业用途，否则将承担因此带来等版权纠纷。
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:2853810243 422108995 23625059584
优雅草开源代码地址：https://gitee.com/youyacao  可查阅所有开源项目



 */
namespace App\Services;

use App\Models\Config;
use TencentCloud\Cvm\V20170312\CvmClient;
use TencentCloud\Cvm\V20170312\Models\DescribeInstancesRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Trtc\V20190722\Models\DismissRoomRequest;
use TencentCloud\Trtc\V20190722\Models\RemoveUserRequest;

class AudioService
{
    private $secretId = '';
    private $secretKey = '';
    private $SDKAppID = '';

    public function __construct(){

        $config = Config::getConfig('audio');
        if (empty($config)){
            throw new \Exception('直播配置错误');
        }

        if($config){
            $this->domain = $config['live_domain'];
            $this->push_domain = $config['live_push_domain'];
            $this->push_key = $config['live_push_key'];
            $this->secretId = $config['audio_accessId'];
            $this->secretKey = $config['audio_secretkey'];
            $this->AppName = $config['audio_id'];
        }
    }

    /**
     * 移出用户
     */
    public function removeUser($roomId, array $userIds)
    {
        try {
            $cred = new Credential($this->secretId, $this->secretKey);
            $client = new CvmClient($cred, "");
            $req = new RemoveUserRequest();
            $req->setSdkAppId($this->SDKAppID);
            $req->setRoomId($roomId);
            $req->setUserIds($userIds);
            $resp = $client->DescribeInstances($req);
            return json_decode($resp->toJsonString(), true);
        } catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }

    /**
     * 解散房间
     */
    public function dismissRoom($roomId)
    {
        try {
            $cred = new Credential($this->secretId, $this->secretKey);
            $client = new CvmClient($cred, "");
            $req = new DismissRoomRequest();
            $req->setSdkAppId($this->SDKAppID);
            $req->setRoomId($roomId);
            $resp = $client->DescribeInstances($req);
            return json_decode($resp->toJsonString(), true);
        } catch(TencentCloudSDKException $e) {
            echo $e;
        }
    }
}
